package com.xiaobaibai.units;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xiaobaibai.bo.BargainDelayItem;
import com.xiaobaibai.entity.BargainUser;
import com.xiaobaibai.service.IBargainUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

/**
 * 对砍价订单的延时队列
 */
@Component
@Slf4j
@Order(999)
public class BargainDelayQueueUnit implements ApplicationRunner {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IBargainUserService bargainUserService;

    private static final String BargainRedisKey="bargainDelayQueue";

    private DelayQueue<BargainDelayItem> delayQueue=new DelayQueue<>();

    /**
     * 存入延时队列,用redis做半持久化
     */
    public void saveBargainDelayItem(BargainDelayItem bargainDelayItem){
        //这里先右吞入redis,再加入队列,好像没区别,因为每个原子都可能断电
        String jsonStr=JSON.toJSONString(bargainDelayItem);
        stringRedisTemplate.opsForList().rightPush(BargainRedisKey,jsonStr);
        delayQueue.add(bargainDelayItem);
    }

    private void consumerBargainItem(){
        while (true){
            try {
                BargainDelayItem bargainDelayItem = delayQueue.take();
                //左吐出吐出redis
                stringRedisTemplate.opsForList().leftPop(BargainRedisKey);//吐出一个任务
                //去修改状态
                UpdateWrapper<BargainUser> updateWrapper=new UpdateWrapper<>();
                updateWrapper.eq("id",bargainDelayItem.getBargainUserId());
                updateWrapper.eq("status",0);//仅仅是修改进行中中的砍价商品
                updateWrapper.set("status",1);//改为失效
                bargainUserService.update(updateWrapper);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("砍价队列异常");
            }
        }
    }

    @Override
    public void run(ApplicationArguments args) {
        //立马从redis中读入延时队列,宕机保护
        List<String> list = stringRedisTemplate.opsForList().range(BargainRedisKey, 0, -1);
        if(list!=null) {
            List<BargainDelayItem> bargainDelayItemList = new ArrayList<>(list.size());
            list.forEach(l -> {
                BargainDelayItem bargainDelayItem = JSON.parseObject(l, BargainDelayItem.class);
                bargainDelayItemList.add(bargainDelayItem);
            });
            delayQueue.addAll(bargainDelayItemList);
        }

        //开始循环
        Thread thread=new Thread(this::consumerBargainItem);
        thread.start();
    }

}
